package com.jn.animationtest_2;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AnimationActivity extends AppCompatActivity implements View.OnClickListener {
   private ImageView mImage;
   private Button mAlphaButton;
   private Button mRotateButton;
   private Button mScaleButton;
   private Button mTranslateButton;
   private Button mSetButton;
   private TextView mText1;
   private TextView mText2;
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_animation);
      mImage = findViewById(R.id.image);
      mAlphaButton = findViewById(R.id.btn_alpha);
      mRotateButton = findViewById(R.id.btn_rotate);
      mScaleButton = findViewById(R.id.btn_scale);
      mTranslateButton = findViewById(R.id.btn_translate);
      mSetButton = findViewById(R.id.btn_set);
      mText1 = findViewById(R.id.text1);
      mText2 = findViewById(R.id.text2);
      mAlphaButton.setOnClickListener(this);
      mRotateButton.setOnClickListener(this);
      mScaleButton.setOnClickListener(this);
      mTranslateButton.setOnClickListener(this);
      mSetButton.setOnClickListener(this);
      mText2.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.btn_alpha) {
         //startAlphaAnimXML();
         startAlphaAnimJava();
      } else if (id == R.id.btn_rotate) {
         //startRotateAnimXML();
         startRotateAnimJava();
      } else if (id == R.id.btn_scale) {
         //startScaleAnimXML();
         startScaleAnimJava();
      } else if (id == R.id.btn_translate) {
         startTranslateAnimXML();
         //startTranslateAnimJava();
      } else if (id == R.id.btn_set) {
         //startSetAnimXML();
         startSetAnimJava();
      } else if (id == R.id.text2) {
         startText2Anim();
      }
   }

   @Override
   public void onBackPressed() {
      super.onBackPressed();
      //overridePendingTransition(R.anim.no_anim, R.anim.alpha_out);
   }

   private void startText2Anim() {
      AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
      alphaAnimation.setDuration(1000);
      alphaAnimation.setFillAfter(true);
      alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
         @Override
         public void onAnimationStart(Animation animation) {

         }

         @Override
         public void onAnimationEnd(Animation animation) {
            startText1Anim();
         }

         @Override
         public void onAnimationRepeat(Animation animation) {

         }
      });
      mText2.startAnimation(alphaAnimation);
   }

   private void startText1Anim() {
      ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 1);
      scaleAnimation.setDuration(1000);
      scaleAnimation.setFillAfter(true);
      mText1.startAnimation(scaleAnimation);
   }

   private void startSetAnimXML() {
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.animations);
      mImage.startAnimation(animation);
   }

   private void startSetAnimJava() {
      TranslateAnimation translateAnimation = new TranslateAnimation(
              Animation.RELATIVE_TO_SELF, 0,
              Animation.RELATIVE_TO_SELF, 0,
              Animation.RELATIVE_TO_SELF, 0,
              Animation.RELATIVE_TO_SELF, 2.2F);
      ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2,
              Animation.ABSOLUTE, 200,
              Animation.RELATIVE_TO_PARENT, 0.4F);
      AlphaAnimation alphaAnimation = new AlphaAnimation(1.0F, 0.4F);
      RotateAnimation rotateAnimation = new RotateAnimation(120, 240,
              Animation.RELATIVE_TO_SELF, 0.5f,
              Animation.RELATIVE_TO_PARENT, 0.3F);

      AnimationSet animationSet = new AnimationSet(true);
      animationSet.addAnimation(translateAnimation);
      animationSet.addAnimation(scaleAnimation);
      animationSet.addAnimation(alphaAnimation);
      animationSet.addAnimation(rotateAnimation);
      animationSet.setDuration(1000);
      mImage.startAnimation(animationSet);
   }

   private void startTranslateAnimXML() {
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
      mImage.startAnimation(animation);
   }

   private void startTranslateAnimJava() {
      //TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 0);
      TranslateAnimation translateAnimation = new TranslateAnimation(
              Animation.RELATIVE_TO_SELF, 0,
              Animation.RELATIVE_TO_SELF, 0,
              Animation.RELATIVE_TO_SELF, 0,
              Animation.RELATIVE_TO_SELF, 2.2F);

      translateAnimation.setDuration(1000);
      //translateAnimation.setRepeatCount(3);
      translateAnimation.setInterpolator(new LinearInterpolator());
      translateAnimation.setFillAfter(true);
      mImage.startAnimation(translateAnimation);
   }

   private void startScaleAnimXML() {
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
      mImage.startAnimation(animation);
   }

   private void startScaleAnimJava() {
      //ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2);
      //ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2, 200, 200);
      ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2,
              Animation.ABSOLUTE, 200,
              Animation.RELATIVE_TO_PARENT, 0.4F);
      scaleAnimation.setDuration(1000);
      //scaleAnimation.setRepeatCount(4);
      //scaleAnimation.setRepeatMode(Animation.REVERSE);
      scaleAnimation.setInterpolator(new BounceInterpolator());
      mImage.startAnimation(scaleAnimation);
   }

   private void startAlphaAnimXML() {
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
      mImage.startAnimation(animation);
   }

   private void startAlphaAnimJava() {
      AlphaAnimation animation = new AlphaAnimation(1.0F, 0.4F);
      animation.setDuration(1000);
      //animation.setRepeatCount(3);
      //animation.setRepeatMode(Animation.REVERSE);
      animation.setInterpolator(new BounceInterpolator());
      animation.setFillAfter(true);
      mImage.startAnimation(animation);
   }

   private void startRotateAnimXML() {
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
      mImage.startAnimation(animation);
   }

   private void startRotateAnimJava() {
      //RotateAnimation animation = new RotateAnimation(0, 270);
      //RotateAnimation animation = new RotateAnimation(0, 270, 200, 200);
      RotateAnimation animation = new RotateAnimation(120, 240,
              Animation.RELATIVE_TO_SELF, 0.5f,
              Animation.RELATIVE_TO_PARENT, 0.3F);

      //animation.setRepeatCount(3);
      //animation.setRepeatMode(Animation.REVERSE);
      animation.setInterpolator(new BounceInterpolator());
      animation.setDuration(1000);
      mImage.startAnimation(animation);
   }


}
